package com.chixing.controller;

import com.chixing.pojo.Member;
import com.chixing.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/*@Controller
@RequestMapping("member")*/
@RestController
public class MemberController {

    @Autowired
    private MemberService memberService;

    @GetMapping("member/{id}")
    public Member getById(@PathVariable("id") Integer id){
        System.out.println("controller getById:" + id);
        Member member = memberService.getById(id);
        return member;
    }

    @GetMapping("member")
    public List<Member> getAll(){
        System.out.println("controller getAll");
        List<Member> memberList =memberService.getAll();
        return memberList;
    }

    @PostMapping("member")
    public boolean save(Member member){
        System.out.println("controller save:" + member);
        boolean result = memberService.save(member);
        return result;
    }

    @DeleteMapping("member/{id}")
    public boolean delete(@PathVariable("id") Integer id){
        System.out.println("controller delete:" + id);
        boolean result = memberService.delete(id);
        return result;
    }

    @PutMapping("member")
    public boolean update(Member member){
        System.out.println("controller update" + member);
        boolean result = memberService.update(member);
        return result;
    }





   /* @RequestMapping("getById")*//*和index.jsp对应*//*       *//*④ 次级控制器*//*
    //根据主键查询
    public ModelAndView getById(Integer id,String name) {  *//*⑤ 执行当前controller的方法*//*
        System.out.println("MemberController get by id: " + id + ",name: " + name);

        ModelAndView mav = new ModelAndView("a");
        mav.addObject("msg","数据查询成功");

        return mav;
//        return "a"; //a.jsp  ⑥根据返回值跳转页面
    }


    *//**
     * Member 是JavaBean
     * Member member 通过反射传教Member对象
     * 通过反射 调用set方法 赋值
     *
     *//*
    @RequestMapping("save")
    //添加新用户
    public String save(Member member){
        System.out.println("MemberController get by id: " + member);
        return "a";
    }*/
}
